<template>
    <el-dialog
        title="设置价格"
        :visible.sync="showValue"
        append-to-body
        :close-on-click-modal="false"
        width="400px"
        center
        :before-close="clearData"
    >
        <div class="dialog">
            <el-form
                ref="form"
                size="mini"
                :model="form"
                @submit.native.prevent
                :rules="rules"
                label-width="82px"
            >
                <el-form-item label="金额:" prop="money">
                    <input-m :word.sync="form.money"></input-m>
                </el-form-item>
            </el-form>
        </div>
        <span slot="footer">
            <el-button
                type="primary"
                size="mini"
                :loading="loading"
                @click="save"
                >确 定</el-button
            >
        </span>
    </el-dialog>
</template>

<script>
export default {
    props: {
        value: Boolean,
        currRow: Object
    },
    data () {
        var validateNum = (rule, value, callback) => {
            if (value === '' || value === null) {
                callback(new Error('金额必须大于0'))
            } else {
                if (value === 0) {
                    callback(new Error('金额必须大于0'))
                }
                callback()
            }
        }
        return {
            loading: false,
            showValue: false,
            form: {
                money: ''
            },
            rules: {
                money: [
                    { required: true, validator: validateNum, trigger: 'blue' }
                ]
            }
        }
    },
    watch: {
        value (val) {
            this.showValue = val
        },
        showValue (val) {
            if (val) {
                this.form.money = this.currRow.price
            }
            this.$emit('update:value', val)
        }
    },
    methods: {
        clearData () {
            this.$refs['form'].resetFields()
            this.showValue = false
        },
        async save () {
            this.$refs.form.validate(async valid => {
                if (valid) {
                    this.loading = true
                    let res = await this.$axios.post(this.$store.state.ip + '/server/agent/serverinfo/rules/updatePrice', {
                        data: {
                            ruleId: this.currRow.ruleID,
                            price: this.form.money
                        }
                    })
                    let response = res.data
                    if (response) {
                        this.loading = false
                    }
                    if (response.ret === '0') {
                        this.$message({
                            message: '修改成功',
                            type: 'success'
                        })
                        this.$emit('getList')
                        this.clearData()
                    }
                }
            })
        }
    }
}
</script>
<style lang="stylus" rel="stylesheet/stylus" scoped></style>